package src;

import javax.swing.*;
import java.awt.*;

public class ChangePasswordDialog extends JDialog {
    private String cardNumber;
    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField;
    private JPasswordField confirmPasswordField;
    private boolean confirmed = false;

    public ChangePasswordDialog(Frame owner, String cardNumber) {
        super(owner, "修改密码", true);
        this.cardNumber = cardNumber;
        setSize(300, 250);
        setLocationRelativeTo(owner);

        JPanel panel = new JPanel(new GridLayout(5, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 显示卡号
        panel.add(new JLabel("卡号:"));
        panel.add(new JLabel(cardNumber));

        // 输入原密码
        panel.add(new JLabel("原密码:"));
        oldPasswordField = new JPasswordField();
        panel.add(oldPasswordField);

        // 输入新密码
        panel.add(new JLabel("新密码:"));
        newPasswordField = new JPasswordField();
        panel.add(newPasswordField);

        // 确认新密码
        panel.add(new JLabel("确认新密码:"));
        confirmPasswordField = new JPasswordField();
        panel.add(confirmPasswordField);

        // 添加按钮
        JButton confirmButton = new JButton("确认修改");
        JButton cancelButton = new JButton("取消");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel);

        // 添加事件监听器
        confirmButton.addActionListener(e -> {
            if (validateInput()) {
                confirmed = true;
                dispose();
            }
        });

        cancelButton.addActionListener(e -> dispose());

        add(panel);
    }

    private boolean validateInput() {
        String oldPassword = new String(oldPasswordField.getPassword()).trim();
        String newPassword = new String(newPasswordField.getPassword()).trim();
        String confirmPassword = new String(confirmPasswordField.getPassword()).trim();

        if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有密码字段！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        // TODO: 验证原密码是否正确
        if (!oldPassword.equals("123456")) { // 示例：假设原密码为123456
            JOptionPane.showMessageDialog(this, "原密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if (!newPassword.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的新密码不一致！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if (newPassword.length() < 6) {
            JOptionPane.showMessageDialog(this, "新密码长度不能少于6位！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public String getNewPassword() {
        return new String(newPasswordField.getPassword()).trim();
    }
} 